package com.cskaoyan.controller;

import com.cskaoyan.bean.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @author stone
 * @date 2022/04/30 16:01
 */
@RestController
public class ArticleController {


    //{}里面的值写的是占位符的名称
    //@PathVariable注解的value属性中指定占位符的名称
    @RequestMapping("{username}/article/details/{id}")
    public String articleDetail(@PathVariable("username")String name,
                                @PathVariable("id")Integer id) {
        System.out.println("name = " + name);
        System.out.println("id = " + id);
        return "ok";
    }

    //可以直接使用字符串来接收，也可以使用字符串数组来接收
    //当我们使用字符串数组来接收的时候，实际上是将字符串分割为字符串数组 → 根据逗号分隔
    @RequestMapping("header")
    public String header(@RequestHeader("Accept") String[] accept,
                         @RequestHeader("Host") String host) {
        System.out.println("accept = " + Arrays.asList(accept));
        System.out.println("host = " + host);
        return "ok";
    }

    //localhost:8080/login?username=songge&password=ligenli
    @RequestMapping("login")
    public String login(@RequestParam("username") String username1,
                        @RequestParam("password")String password1) {
        return "ok";
    }

    @RequestMapping("cookie")
    public String cookieValue(@CookieValue("songge") String value) {
        return "ok";
    }

    @RequestMapping("session/put/{name}")
    public String sessionPut(HttpSession session,@PathVariable("name") String name) {
        //session.setAttribute("name", name);
        session.setAttribute("user",new User(name,"123456"));
        return "ok";
    }

    //@SessionAttribute和@CookieValue都是根据key来获得value
    //但是CookieValue对应的参数的类型是String
    //SessionAttribute对应的参数的类型是Object → 放入session时值的类型是什么，就可以以什么类型取出
    //也就是可以直接修改形参的类型
    @RequestMapping("session/get")
    public String sessionGet(@SessionAttribute("user")User user) {
        System.out.println("user = " + user);
        return "ok";
    }
}
